package com.lipuwater.agent.ui.activity.sales;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ismartlib.utils.NetUtils;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseLayoutActivity;
import com.lipuwater.agent.http.ApiCallBack;
import com.lipuwater.agent.http.HttpConfig;
import com.lipuwater.agent.http.ResponseParams;
import com.lipuwater.agent.http.api.ApiRequestsBasis;
import com.lipuwater.agent.http.bean.sales.SalesDesBean;
import com.lipuwater.agent.ui.dialog.QRCodeDialog;
import com.lipuwater.agent.utils.MapToBeanUtil;
import com.lipuwater.agent.utils.onclick.NoDoubleClick;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import mehdi.sakout.fancybuttons.FancyButton;
import okhttp3.Call;


public class SalesActivity extends BaseLayoutActivity {

    @Bind(R.id.text_order_code)
    TextView textOrderCode;
    @Bind(R.id.text_client_name)
    TextView textClientName;
    @Bind(R.id.text_mobile_phone)
    TextView textMobilePhone;
    @Bind(R.id.text_address)
    TextView textAddress;
    @Bind(R.id.text_sales_user)
    TextView textSalesUser;
    @Bind(R.id.btn_collection)
    FancyButton btnCollection;
    @Bind(R.id.text_goods_name)
    TextView textGoodsName;
    @Bind(R.id.text_goods_phone)
    TextView textGoodsPhone;

    private int orderId;
    private SalesDesBean mSalesDes;
    private NoDoubleClick mNoDoubleClick = new NoDoubleClick();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_sales);
        toolbar.setBackgroundResource(R.drawable.img_title_bar);
        initTitle(R.drawable.icon_aleft_white, "销售单详情");
        toolbarTitle.setTextColor(Color.parseColor("#ffffff"));
        ButterKnife.bind(this);
        showLoading();
        orderId = getIntent().getIntExtra("orderId", 0);
        getSalesDes();
        setReloadClickListener();
    }

    @OnClick({R.id.layout_order_des, R.id.layout_settle_des, R.id.layout_qrcode, R.id.btn_collection})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_order_des:
                if (mNoDoubleClick.check()) return;
                if (mSalesDes != null) {
                    Intent intent = new Intent(this, SalesOrderActivity.class);
                    intent.putExtra("orderList", mSalesDes.getOrderInfo());
                    startActivity(intent);
                } else {
                    Toast("没有订单信息");
                }
                break;
            case R.id.layout_settle_des:
                if (mNoDoubleClick.check()) return;
                if (mSalesDes != null) {
                    Intent intent = new Intent(this, SalesSettleListActivity.class);
                    intent.putExtra("SettlementList", mSalesDes.getSettlementInfo());
                    startActivity(intent);
                } else {
                    Toast("暂时没有结算内容");
                }
                break;
            case R.id.layout_qrcode:
                if (mNoDoubleClick.check()) return;
                if (mSalesDes != null) {
                    new QRCodeDialog(this, mSalesDes.getQrcode()).show();
                }
                break;
            case R.id.btn_collection:
                if (mNoDoubleClick.check()) return;
                if (btnCollection.getVisibility() == View.VISIBLE && mSalesDes != null) {
                    if (mSalesDes.getState() == 6) {
                        Toast("请提醒买家在微信确认订单信息");
                        return;
                    }
                }
                btnCollection.setClickable(false);
                updateOrderState();
                break;
        }
    }

    private void getSalesDes() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("orderId", orderId);
        ApiRequestsBasis.getSalesDes(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    mSalesDes = (SalesDesBean) MapToBeanUtil.toJavaBean(result.getParams().get("salesInfo"), SalesDesBean.class);
                    setData(mSalesDes);
                } else {
                    showNoNetWork();
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                showNoNetWork();
                if (NetUtils.isConnected(SalesActivity.this)) {
                    Toast(getString(R.string.service_error));
                } else {
                    Toast(getString(R.string.network_fails));
                }
            }
        });
    }

    private void setData(SalesDesBean salesDes) {
        textOrderCode.setText(salesDes.getOrderCode());
        textClientName.setText(salesDes.getClientName());
        textMobilePhone.setText(salesDes.getMobilePhone());
        textAddress.setText(salesDes.getGoodsAddress());
        textSalesUser.setText(salesDes.getSalesStaff());
        textGoodsName.setText(salesDes.getRecipientName());
        textGoodsPhone.setText(salesDes.getRecipientMobile());
        if (salesDes.getState() == 5) {
            btnCollection.setVisibility(View.VISIBLE);
        }
        hideEmptyLayout();
    }

    private void updateOrderState() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);
        params.put("orderId", orderId + "");
        ApiRequestsBasis.updateOrderState(this, params, new ApiCallBack() {
            @Override
            public void onResponse(ResponseParams result) {
                if (result.getStatus() == HttpConfig.success) {
                    btnCollection.setVisibility(View.GONE);
                    Toast("已经确认收款");
                } else {
                    btnCollection.setClickable(true);
                    Toast(result.getMessage());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                btnCollection.setClickable(true);
                if (NetUtils.isConnected(SalesActivity.this)) {
                    Toast(getString(R.string.service_error));
                } else {
                    Toast(getString(R.string.network_fails));
                }
            }
        });
    }

    private void setReloadClickListener() {
        emptyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLoading();
                getSalesDes();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
